package bzapps.weynak;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class LaunchUpdateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent service = new Intent(context, UpdateLocationService.class);
        PendingIntent updateLocation = PendingIntent.getService(context, 0, service, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(updateLocation);

        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);

        boolean update = pref.getBoolean("weynak.update_location", true);
        boolean privateEnable = pref.getBoolean("privacycheck", false);
        String time = pref.getString("weynak.time.interval", "1800000"); // 30min

        if (update && !privateEnable) {
            alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, Long.parseLong(time), updateLocation);
        }
                
    }
}
